package com.intel.factorytest.menuitem;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.StatFs;
import android.util.Log;



public class ServiceTestsItemMenu {
    private String sysTestItem = "1111111111111111111111111111";
    private int i =0;
    private String TAG ="ServiceTestsItemMenu"; 
    private int line_min = sysTestItem.length(); 
    private String config_data = "mat_test_case_config"; 
    private String config_data_for_pcba = "mat_test_case_config_for_pcba"; 
    private Context mContext = null;

    public ServiceTestsItemMenu(Context context) {
        mContext = context;
    }

    /**
     * read configuration file to initialize sysTestItem
     * 
     * @param fileName
     */
    public void readFileByLines(String fileName) {
        String s = "";
        //File file = new File(fileName);
        BufferedReader reader = null;
        InputStream inputStream = null;
        /*if (!file.exists()) {
            Log.i(TAG, "read file " + fileName + " fail !");
            return;
        }*/

        Log.i(TAG, "line_min " + line_min);

        try {
            Log.i(TAG, "read file by line, once one line");
            //reader = new BufferedReader(new FileReader(file));
            if(mContext == null)return;
            inputStream = mContext.getResources().getAssets().open(fileName);
            if(inputStream == null) {
                Log.i(TAG, "inputStream == null.\n");
                return;

            }
            reader = new BufferedReader(new InputStreamReader(inputStream));
            String tempString = null;
            int line = 1;        // valid line

            // line by line read, until null
            while ((tempString = reader.readLine()) != null) {
                // show line information
                Log.i(TAG, "line " + line + ": " + tempString);

                if (tempString.length() != 0)
                    tempString = tempString.substring(0, 1);

                if (tempString.equals("0") || tempString.equals("1")) {
                    s = s.concat(tempString);
                    line++;
                }
            }

            if ((line - 1) == line_min)
                sysTestItem = s;
            else 
                Log.i(TAG, "file format wrong.\n");

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public boolean readSDConfigByLines(String fileName) {
        Log.i(TAG, "readSDConfigByLines fileName = " + fileName);
        boolean result = true;
        String s = "";
        if("".equals(fileName))return false;
        File file = new File(fileName);
        BufferedReader reader = null;
        if (!file.exists()) {
            Log.i(TAG, "read file " + fileName + " fail !");
            return false;
        }

        Log.i(TAG, "line_min " + line_min);

        try {
            Log.i(TAG, "read file by line, once one line：");
            reader = new BufferedReader(new FileReader(file));
            if(mContext == null)return false;
            String tempString = null;
            int line = 1;        // valid line

            // line by line read, until null
            while ((tempString = reader.readLine()) != null) {
                // show line information
                Log.i(TAG, "line " + line + ": " + tempString);

                if (tempString.length() != 0)
                    tempString = tempString.substring(0, 1);

                if (tempString.equals("0") || tempString.equals("1")) {
                    s = s.concat(tempString);
                    line++;
                }
            }

            if ((line - 1) == line_min)
                sysTestItem = s;
            else {
                Log.i(TAG, "readSDConfigByLines file format wrong.\n");
                result = false;
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return result;
    }
     public boolean isGetSDConfigPath(String path, int isSysForPcba) {
        File file = null;
        //path = "/storage/sdcard1";
        Log.i(TAG, "isGetSDConfigPath path = " + path);
        String filePath = "";
        if(1 == isSysForPcba) {
            file = new File("/mnt/sdcard_ext" + "/" + config_data_for_pcba );
        } else {
            file = new File("/mnt/sdcard_ext" + "/" + config_data);
        }
        StatFs stat = null;
        if(file.exists()) {
            if(1 == isSysForPcba) {
                filePath = "/mnt/sdcard_ext" + "/" + config_data_for_pcba;
            } else {
                filePath = "/mnt/sdcard_ext" + "/" + config_data;
                
            }
            stat = new StatFs(filePath);
            
        } else {
            if(!path.equals("")) {
                if(1 == isSysForPcba) {
                    file = new File(path + "/" + config_data_for_pcba );
                } else {
                    filePath = path + "/" + config_data;
                    file = new File(filePath);
                }
                Log.i(TAG, "file.exists = " + file.exists() + "  file name = " + file.getName());
                if(file.exists()) {
                    if(1 == isSysForPcba) {
                        filePath = path + "/" + config_data_for_pcba;
                        
                    } else {
                        filePath = path + "/" + config_data;
                     }
                    stat = new StatFs(filePath);
                } else {
                    filePath =  "/storage/" + path.substring(path.lastIndexOf("/") + 1);
                    
                    if(1 == isSysForPcba) {
                        filePath = filePath + "/" + config_data_for_pcba;
                        
                    } else {
                        filePath = filePath + "/" + config_data;
                     }
                    Log.i(TAG, "filePath = " + filePath);
                    file = new File(filePath);
                    if(file.exists()) {

                        stat = new StatFs(filePath);
                    }
                }
            }

        }
        if(stat == null) {        Log.i(TAG, "stat == null");
            return false;

        }
        final float totalBlocks = stat.getBlockCount();
        if(totalBlocks == 0) {Log.i(TAG, "totalBlocks == 0");
            return false;
        }
        if(!readSDConfigByLines(filePath)) return false;
        return true;

    }

    public List<Map<String, Object>> showTestsMenuMap(int isSys, int isSysForPcba, String localeString, String sdPath)
    {
        List<Map<String, Object>> testItemMenu = new ArrayList<Map<String, Object>>();
        java.util.Set<TestsItemInformation> set = null;
        java.util.Set<TestsItemInformationCN> setCN = null;
        java.util.Iterator it = null;
        if(localeString.equals("CN")) {
            setCN = java.util.Collections.synchronizedSet(java.util.EnumSet.allOf(TestsItemInformationCN.class));
            it = setCN.iterator();
        } else {
            set = java.util.Collections.synchronizedSet(java.util.EnumSet.allOf(TestsItemInformation.class));
            it = set.iterator();
        }
            Log.i(TAG, "showTestsMenuMap");
        if(!isGetSDConfigPath(sdPath, isSysForPcba)) {
            Log.i(TAG, "showTestsMenuMap false");
            if(1 == isSysForPcba) {
                readFileByLines(config_data_for_pcba);
            } else {
                readFileByLines(config_data);
            }
        }
        Log.i(TAG, "sysTestItem: " + sysTestItem);
        while(it.hasNext())
        {
            char a = sysTestItem.charAt(i);
            i++;
            if (isSys == 1 && a =='0') {
                it.next();
            }else {
                TestsItemInformation l = null;
                TestsItemInformationCN lCN = null;
                if(localeString.equals("CN")) {
                    lCN = (TestsItemInformationCN) it.next();
                } else {
                    l = (TestsItemInformation) it.next();
                }
                Map<String, Object> testItem = new HashMap<String,Object>();
                Intent bIntent = new Intent();
                if(localeString.equals("CN")) {
                    bIntent.setClassName(lCN.package_name, lCN.class_name);
                    testItem.put("itemName", lCN.item_name);
                } else {
                    bIntent.setClassName(l.package_name, l.class_name);
                    testItem.put("itemName", l.item_name);
                }
                testItem.put("intent", bIntent);
                testItemMenu.add(testItem);
            }
        }


        return testItemMenu;
    }    
}
